using System.Collections.Generic;
using JetBrains.Annotations;
using JetBrains.Application.Parts;
using JetBrains.ForTea.RiderPlugin.TemplateProcessing.Managing.Impl;
using JetBrains.IDE;
using JetBrains.IDE.PerClientComponents;
using JetBrains.ProjectModel;
using JetBrains.RdBackend.Common.Features.Documents;
using JetBrains.ReSharper.Feature.Services.Protocol;
using JetBrains.Rider.Model;
using JetBrains.Util;

namespace JetBrains.ForTea.RiderPlugin.ProtocolAware.Impl
{
  [SolutionPerClientComponent(Instantiation.DemandAnyThreadSafe)]
  public sealed class T4HostOutputFileRefresher : T4BasicOutputFileRefresher
  {
    public T4HostOutputFileRefresher(ISolution solution) : base(solution)
    {
    }

    public override void Refresh(IProjectFile output)
    {
      base.Refresh(output);
      SyncDocuments(output.Location);
      RefreshFiles(output.Location);
    }

    private void SyncDocuments([NotNull] VirtualFileSystemPath destinationLocation) =>
      Solution.SyncDocumentsWithFiles(destinationLocation);

    private void RefreshFiles([NotNull] VirtualFileSystemPath destinationLocation) => Solution
      .GetProtocolSolution()
      .GetFileSystemModel()
      .RefreshPaths
      .Start(new RdFsRefreshRequest(new List<RdPath> { destinationLocation.ToRd() }, true));
  }
}